
import XEUtils from 'xe-utils'
import { TokenUtils, AUTH_CONS } from '@/utils/auth/index.js'

export function reqResolve(config) {
  // 添加防重放的随机数
  config.headers['t'] = +new Date()
  // 验证token是否存在，并携带请求头
  const accessToken = TokenUtils.getAuthorization()
  if (!XEUtils.isEmpty(accessToken)) {
    config.headers[AUTH_CONS.REQ_HEADER_KEY] = accessToken
  }
  return config
}
export function reqReject(error) {
  console.error(error)
  return Promise.reject(error)
}
export function resResolve(response) {
  // 设置刷新token
  const { refreshToken } = response.headers
  if (refreshToken) { TokenUtils.setAuthorization(refreshToken) }
  // 处理响应数据
  let responseResult = null
  const responseData = response.data
  responseResult = responseData
  return responseResult
}
export function resReject(error) {
  if (error.code === 'ECONNABORTED' || error.message === "Network Error" || error.message.includes("timeout")) {
    console.error(error)
  }
  console.error(error)
  return Promise.reject(error)
}
